import java.awt.Graphics;
import java.util.*;
import java.math.*;

//Goal sprite is a type of mobile sprite that moves toward a goal
public abstract class GoalSprite extends MobileSprite {
	private Sprite target;
	
	public void setTarget(Sprite newTarget)
	{
		target = newTarget;
	}

	/**
	 * Move returns false if it doesn't move this turn, and an error message is printed
	 * if the Sprite has no target.  Otherwise it returns true and moves toward the target. 
	 */
	public boolean move(HexGrid world) {
		moveCount = 0;
		int [] lastLocation = getLocation();
		if(target == null)
		{
			//Throw an Exception?
			System.out.println("Sprite has not been assigned a target");
			return false;
		}
		int goal[] = target.getLocation();
		int currentLocation[] = getLocation();
		oldLocation = lastLocation; //Barring errors even if we stay in the Hex, the lastLocation changes to this cell               
		if(goal == currentLocation)
		{
			return false;
		}
		else
		{
			//Move closer to the target Sprite
			Collection<Hex> neighbors = world.get_neighbors(currentLocation);
			Hex choice = null;
			double best_dist = 0;
			int tar_loc[] = this.target.getLocation();
			//Simple heuristic -Nick
			for(Hex n:neighbors){
				int tmp[] = n.getLocation();
				double dist = Math.sqrt( Math.pow((tmp[0]-tar_loc[0]),2) + Math.pow(tmp[1]-tar_loc[1],2) );
				//For each neighbor, check to see if it gets you closer to the goal
				if(choice == null){
					choice = n;
					best_dist = dist;
				}
				else if(dist < best_dist){
					choice = n;
					best_dist = dist;
				}
			}
			
			if(choice == null){return false;}
			else{
			    //System.out.println("old" + world.get_hex(this.getLocation()).toString());
			    //System.out.println(choice.toString());
				world.get_hex(this.getLocation()).leaveHex();
				choice.moveIntoHex(this);
				setParameters(world);
				moved = true;
	    		return moved;
			}
		}
		
		
	}

}
